

module.exports = app => {
    return class HyperbahnClient extends app.Service {

    // *  getkeyThriftChannel() {
    //     var fs = require('fs');
    //     var path = require('path');
    //     var TChannel = require('tchannel');
    //     var HyperbahnClient = require('tchannel/hyperbahn/');
    //     var rootChannel = TChannel();

    //     var hyperbahnClient = HyperbahnClient({
    //         tchannel: rootChannel,
    //         serviceName: 'keyvalue-client',
    //         hostPortList: ['172.30.32.88:5011', '172.30.32.88:3012', '172.30.32.88:3013'],
    //         hardFail: true,
    //         reportTracing: false,
    //     });


    //     var keyChan = hyperbahnClient.getClientChannel({
    //          serviceName: 'core_order_service'
    //     });

    //     console.log(keyChan);

    //     var keyThrift = rootChannel.TChannelAsThrift({
    //         entryPoint: path.join(__dirname, 'thrift/core_order.thrift'),
    //         channel: keyChan
    //     });
    //     return keyThrift;
    //   }
    // }

     * requestThriftChannel(serName,obj) {
     //   return null;
         var fs = require('fs');
        var path = require('path');
        var TChannel = require('tchannel');
        var HyperbahnClient = require('tchannel/hyperbahn/');
        var rootChannel = TChannel();

        var hyperbahnClient = HyperbahnClient({
            tchannel: rootChannel,
            serviceName: 'keyvalue-client',
            hostPortList: ['172.30.32.88:5011', '172.30.32.88:3012', '172.30.32.88:3013'],
            hardFail: true,
            reportTracing: false,
        });


        var keyChan = hyperbahnClient.getClientChannel({
             serviceName: serName // 'core_order_service'
        });

        console.log(keyChan);

        var keyThrift = rootChannel.TChannelAsThrift({
            entryPoint: path.join(__dirname, 'thrift/core_order.thrift'),
            channel: keyChan
        });
         keyThrift.request({
                 // serviceName: 'process_config_service',
                 timeout: 10000,
                 hasNoParent: true
              }).send('OrderServerService::InsertOrder',null , {
                   orderbean:obj
                 }, function onResponse(err, resp) {
                    if (err) {
                        console.log('got error', err);
                        return err;
                    } else {
                        console.log('got response', resp.body);
                        return resp.body;
                    }
                    //rootChannel.close();
                });
      }
    }
}

